<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="房屋编号" prop="houseId">
        <el-input
          v-model="queryParams.houseId"
          placeholder="请输入房屋编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="报障部位" prop="reportDetail">
        <el-select
          v-model="queryParams.reportDetail"
          placeholder="报障部位"
          clearable
          size="small"
          style="width: 240px"
        >
          <el-option
            v-for="dict in dict.type.house_report_detail"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="reportList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="报障编号" align="center" prop="reportId"/>
      <el-table-column label="房屋编号" align="center" prop="houseId"/>
      <el-table-column label="报障部位" align="center" prop="reportDetail">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.house_report_detail" :value="scope.row.reportDetail"/>
        </template>
      </el-table-column>
      <el-table-column label="处理状态" align="center" prop="isHandle">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.house_is_handle" :value="scope.row.isHandle"/>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['domain:report:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-thumb"
            @click="handleReport(scope.row)"
            v-hasPermi="['domain:report:handle']"
            v-if="scope.row.isHandle == 0"
          >处理
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-document"
            @click="reportDetail(scope.row)"
            v-hasPermi="['domain:report:query']"
          >报障详情
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['domain:report:remove']"
            v-if="scope.row.isHandle == 1"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!--修改用户报障对象对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="550px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="房屋编号" prop="houseId">
          <el-input v-model="form.houseId" disabled/>
        </el-form-item>
        <el-form-item label="报障人编号" prop="userId">
          <el-input v-model="form.userId" disabled/>
        </el-form-item>
        <el-form-item label="处理状态" prop="isHandle">
          <el-radio-group v-model="form.isHandle" disabled>
            <el-radio
              v-for="dict in dict.type.house_is_handle"
              :key="dict.value"
              :label="dict.value"
            >{{ dict.label }}
            </el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="报障部位" prop="reportDetail">
          <el-select v-model="form.reportDetail" placeholder="请选择报障部位">
            <el-option
              v-for="dict in dict.type.house_report_detail"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入备注"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!--报障详情对话框 -->
    <el-dialog :title="title" :visible.sync="showDetail" width="600px" append-to-body>
      <el-form ref="form" :model="detail" label-width="150px">
        <el-form-item label="房屋编号" prop="houseId">
          <el-input v-model="detail.house.houseId" disabled/>
        </el-form-item>
        <el-form-item label="房屋名称" prop="houseName">
          <el-input v-model="detail.house.houseName" disabled/>
        </el-form-item>
        <el-form-item label="报障人编号" prop="userId">
          <el-input v-model="detail.user.userId" disabled/>
        </el-form-item>
        <el-form-item label="报障人昵称" prop="nickName">
          <el-input v-model="detail.user.nickName" disabled/>
        </el-form-item>
        <el-form-item label="处理状态" prop="isHandle">
          <el-radio-group v-model="detail.report.isHandle" disabled>
            <el-radio
              v-for="dict in dict.type.house_is_handle"
              :key="dict.value"
              :label="dict.value"
            >{{ dict.label }}
            </el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="报障部位" prop="reportDetail">
          <el-select v-model="detail.report.reportDetail" disabled>
            <el-option
              v-for="dict in dict.type.house_report_detail"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="detail.report.remark" type="textarea" disabled/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listReport, getReport, delReport, updateReport, getReportDetail, handleReport} from "@/api/system/report";

export default {
  name: "Report",
  dicts: ["house_is_handle", "house_report_detail"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用户报障对象表格数据
      reportList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      //是否打开报障详情层
      showDetail: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        houseId: null,
        reportDetail: null,
        isHandle: null
      },
      // 表单参数
      form: {},
      //报障详情
      detail: {
        house: {},
        user: {},
        report: {}
      },
      // 表单校验
      rules: {}
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询用户报障对象列表 */
    getList() {
      this.loading = true;
      const url = window.location.pathname;
      const accessPath = url.substr(url.lastIndexOf("/"), url.length);
      //判断当前页面
      switch (accessPath) {
        //中介
        case "/m_unhandle":
          this.queryParams.isHandle = 0;
          break;
        case "/m_handled":
          this.queryParams.isHandle = 1;
          break;
        //租客
        case "/unhandle":
          this.queryParams.isHandle = 0;
          break;
        case "/handled":
          this.queryParams.isHandle = 1;
          break;
        default:
          break;
      }
      listReport(this.queryParams).then(response => {
        this.reportList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.showDetail = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        reportId: null,
        houseId: null,
        userId: null,
        reportDetail: null,
        createTime: null,
        updateTime: null,
        status: "0",
        remark: null,
        isHandle: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.reportId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const reportId = row.reportId || this.ids
      getReport(reportId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改用户报障对象";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.reportId != null) {
            updateReport(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const reportIds = row.reportId || this.ids;
      this.$modal.confirm('是否确认删除用户报障对象编号为"' + reportIds + '"的数据项？').then(function () {
        return delReport(reportIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    //中介处理报障
    handleReport(row) {
      const report = row;
      this.$modal.confirm("是否确认处理改报障信息").then(function () {
        return handleReport(report);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("处理成功");
      }).catch(() => {
      });
    },
    //查看报障详情
    reportDetail(row) {
      getReportDetail(row.reportId, row.houseId, row.userId).then(response => {
        this.detail.house = response.house;
        this.detail.user = response.user;
        this.detail.report = response.report;
        this.showDetail = true;
        this.title = "报障详情"
      })
    }
  }
};
</script>
